<template>
  <div id="app">
    <!-- 导航栏组件 -->
    <nav-view></nav-view>

    <!-- 路由视图 -->
    <keep-alive include="search">
      <router-view class="view" @play1="play" @play2="play"></router-view>
    </keep-alive>

    <!-- 播放栏 -->
    <div class="bofang" @click.stop="showPlay">
      <div class="box" @click.stop="showPlay">
        <div class="pic">
          <img
            :src="`${currentMusic.picUrl || currentMusic.al.picUrl}`"
            :class="{ myaround: isPlay }"
          />
        </div>
        <div class="songName" @click.stop="showPlay">
          {{ currentMusic.name }}
        </div>
        <div class="togglePlay">
          <div class="left" @click.stop="upOne">
            <van-icon name="arrow-left" size="25px" />
          </div>
          <div class="cen" @click.stop="orPlay">
            <van-icon
              name="pause-circle-o"
              size="30px"
              :class="{ myplay: !isPlay }"
            />
            <van-icon
              name="play-circle-o"
              size="30px"
              :class="{ myplay: isPlay }"
            />
          </div>
          <div class="right" @click.stop="nextOne">
            <van-icon name="arrow" size="25px" />
          </div>
        </div>
      </div>

      <audio
        :src="`https://music.163.com/song/media/outer/url?id=${currentMusic.id}.mp3`"
        controls
        autoplay
        class="palyAudio"
        ref="audioPlay"
        @timeupdate="getCurrentTime"
        @click.stop="showPlay"
        @ended="addAutoPlay"
        muted
      ></audio>
    </div>

    <!-- 音乐播放页面 -->
    <transition name="move">
      <full-screen
        v-if="isShowPlay"
        @showPlay="showPlay"
        ref="full"
      ></full-screen>
    </transition>

    <div class="bottomBox"></div>
  </div>
</template>

<script>
import navView from "./components/nav.vue";
import fullScreen from "./components/fullScreen";
export default {
  data() {
    return {
      // 是否展示播放页面
      isShowPlay: false,
    };
  },
  computed: {
    // 当前播放时间
    currentMusic: {
      get() {
        return this.$store.state.currentMusic;
      },
      set() {},
    },
    // 播放状态
    isPlay: {
      get() {
        return this.$store.state.isPlay;
      },
      set() {},
    },
    // 播放列表
    playList: {
      get() {
        return this.$store.state.playMusicList;
      },
      set() {},
    },
    // 音量
    volume: {
      get() {
        return this.$store.state.volume;
      },
      set() {},
    },
    // 歌曲当前播放时间
    currentTime:{
      get(){
        return this.$store.state.currentTime
      }
    },
    // 改变的播放时间
    changeCurrentTime:{
      get(){
        return this.$store.state.changeCurrentTime
      },
      set(){}
    }
  },
  components: {
    navView,
    fullScreen,
  },
  methods: {
    // 把歌曲放进vuex的当前播放歌曲和播放列表
    play(music) {
      this.$store.commit("UPDATACURENTMUSIC", music);
      this.$store.commit("UPDATAPLAYMUSICLIST", music);
      this.$store.commit("GETVOLUME", this.$refs.audioPlay.volume);
    },

    // 是否显示fullScreen组件
    showPlay() {
      if (this.currentMusic.id) {
        this.isShowPlay = !this.isShowPlay;
        // console.log(this.isShowPlay);
      } else {
        return;
      }
    },
    // 改变播放状态
    audioPlay() {
      if (this.isPlay) {
        //暂停状态转播放
        this.$refs.audioPlay.play();
        this.getCurrentTime();
      } else {
        // 播放状态转暂停
        this.$refs.audioPlay.pause();
        clearInterval(this.interVal);
      }
    },
    // 上一首
    upOne() {
      if (this.playList[1]) {
        this.$store.commit("OUTUP");
      } else {
        return;
      }
    },
    
    orPlay() {
      if (this.currentMusic.id) {
        this.$store.commit("UPDATAISPLAY");
      } else {
        return;
      }
    },
    // 下一首
    nextOne() {
      if (this.playList[1]) {
        this.$store.commit("OUTNEXT");
      } else {
        return;
      }
    },
    // 自动循环事件监听
    addAutoPlay() {
      // console.log(this.$refs.audioPlay);
      this.$refs.audioPlay.loop = false;
      this.$store.commit("OUTNEXT")
      // console.log("播放结束");
      // console.log("full =>", this.$refs.full);
      // this.$refs.full.downOne();
      // this.$refs.audioPlay.addEventListener("ended", () => {
      //   // console.log("000");
      //   this.$refs.audioPlay.downOne();
      // });
    },

    // 获取歌曲播放时间
    getCurrentTime() {
      // console.log("000");
      this.$store.commit("GETCURRENTIME", this.$refs.audioPlay.currentTime)
    },
  },
  watch: {
    isPlay: {
      handler() {
        this.audioPlay();
      },
    },
    currentMusic: {
      handler() {
        this.isaround = this.isPlay;
        // this.getVolume()
        // 当前播放音乐变化时获取当前音乐时长
        setTimeout(() => {
          this.$store.commit('GETDURATION',this.$refs.audioPlay.duration)
        }, 300)
      },
    },
    volume: {
      handler(nv, ov) {
        if (nv == ov) {
          // console.log("000");
          return;
        } else {
          this.$refs.audioPlay.volume = nv / 100;
        }
      },
    },
    changeCurrentTime:{
      handler(nv){
        this.$refs.audioPlay.currentTime=nv
      }
    }
  },
};
</script>

<style lang="scss" scoped>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  font: 14px/1.5 Helvetica, sans-serif, STHeiTi;

  .view {
    margin-top: 40px;
  }

  .bofang {
    position: absolute;
    bottom: -10px;
    z-index: 20;
    .box {
      width: 100%;
      height: 50px;
      background-color: #f1f3f4;
      position: fixed;
      bottom: 0px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .pic {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
      .songName {
        width: 50vw;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .togglePlay {
        display: flex;
        justify-content: center;
        align-items: center;
        div {
          margin: 0 1vw;
        }
      }
    }
    .palyAudio {
      display: none;
    }
  }

  .bottomBox {
    height: 50px;
    width: 100%;
    background-color: #fff;
  }
}
.move-enter,
.move-leave-to {
  transform: translateY(100%);
}
.move-enter-active,
.move-leave-active {
  transition: all 0.2s linear;
}
.move-enter-to,
.move-leave {
  transform: translateY(0);
}
@keyframes around {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
.myaround {
  animation: around 5s linear infinite;
}
.myplay {
  display: none;
}
</style>
